package cn.itcast.function;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class FunctionTest {

    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> biFunction = (i1, i2) -> i1 + i2;

        System.out.println(biFunction.apply(1, 2));
    }

    private static void predicate() {
        Predicate<Integer> predicate = integer -> {
            if (integer > 3) {
                return true;
            } else {
                return false;
            }
        };

        Function<Integer, Boolean> function = integer -> {
            if (integer > 3) {
                return true;
            } else {
                return false;
            }
        };
    }

    private static void identity() {
        //Supplier去返回一个Function的identity方法
        Supplier<Function> supplier = Function::identity;

        //输出自己 --  t -> t
        System.out.println(supplier.get().apply(1));
    }

    private static void andThen() {
        //接受一个值，返回一个Boolean值
        Function<Integer, Integer> function1 = i -> {
            if (i > 4) {
                return 1;
            } else {
                return 2;
            }
        };
        Function<Integer, Integer> function2 = i -> i * i;

        //先执行f1,在执行f2，2*2
        System.out.println(function1.andThen(function2).apply(1));
        //先执行f2,在执行f1，2   -> 1
        System.out.println(function2.andThen(function1).apply(1));
    }

    private static void compose() {
        //接受一个值，返回一个Boolean值
        Function<Integer, Integer> function1 = i -> {
            if (i > 4) {
                return 1;
            } else {
                return 2;
            }
        };
        Function<Integer, Integer> function2 = i -> i * i;
        //在function2执行之前，先执行function1,猜测一下  1小于4返回2，2*2 = 4
        Integer integer = function2.compose(function1).apply(1);
        System.out.println(integer);


        //在function1执行之前，先执行function2,猜测一下  1*1 = 1 ,比4小返回2
        Integer integer2 = function1.compose(function2).apply(1);
        System.out.println(integer2);
    }

    private static void apply() {
        //接受一个值，返回一个Boolean值
        Function<Integer, Boolean> function = (i) -> {
            if (i > 1) {
                return true;
            } else {
                return false;
            }
        };

        Boolean result = function.apply(3);

        System.out.println(result);
    }
}